﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ImageConverter.Properties;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace ImageConverter
{
    public partial class frmProcess : Form
    {
        private Settings setting;
        private bool cancel = false;
        public frmProcess()
        {
            InitializeComponent();
            setting = new Settings();
        }

        private void btnBrowseConverter_Click(object sender, EventArgs e)
        {
            if (fbImageConverter.ShowDialog(this) == DialogResult.OK)
            {
                txtImageConverter.Text = Path.GetFileName(fbImageConverter.FileName);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtImageConverter.Text = setting.exeLocation.Trim();
            txtArgument.Text = setting.argument.Trim();
            txtSourceExtension.Text = setting.sourceExtension.Trim();
            txtDestinationExtension.Text = setting.destinationExtension.Trim();
            txtSourceFolder.Text = setting.folderSource.Trim();
        }

        private void btnBrowseSourceFolder_Click(object sender, EventArgs e)
        {
            if (fbSourceFolder.ShowDialog(this) == DialogResult.OK)
            {
                txtSourceFolder.Text = fbSourceFolder.SelectedPath;
            }
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {

            setting.exeLocation = txtImageConverter.Text.Trim();
            setting.argument = txtArgument.Text.Trim();
            setting.sourceExtension = txtSourceExtension.Text.Trim();
            setting.destinationExtension = txtDestinationExtension.Text.Trim();
            setting.folderSource = txtSourceFolder.Text.Trim();
            setting.Save();

            cancel = false;

            string[] files = Directory.GetFiles(txtSourceFolder.Text.Trim());
            pbCurrentProgress.Maximum = files.Length;
            int i = 0;
            int all = pbCurrentProgress.Maximum;
            pbCurrentProgress.Value = 0;
            string sourceImage = "";
            string newImage = "";
            Process process=new Process();
            for (i = 0; i < all; i++)
            {
                if (cancel)
                {
                    return;
                }
                try
                {
                    if (Path.GetExtension(files[i]).Equals(txtSourceExtension.Text.Trim()))
                    {
                        sourceImage = Path.GetFileName(files[i]);
                        newImage = Path.GetFileNameWithoutExtension(files[i])
                            + "." + txtDestinationExtension.Text.Trim();

                        File.Move(txtSourceFolder.Text + "\\" + sourceImage, 
                            txtSourceFolder.Text + "\\" + sourceImage.Replace(" ", ""));

                        ProcessStartInfo info = new ProcessStartInfo();                        
                        info.Arguments = sourceImage.Replace(" ","") + " " 
                            + txtArgument.Text.Trim() + " " 
                            + newImage.Replace(" ","");
                        info.CreateNoWindow = true;
                        info.WindowStyle = ProcessWindowStyle.Hidden;
                        info.WorkingDirectory = txtSourceFolder.Text;
                        info.FileName = txtImageConverter.Text;

                        process.StartInfo = info;
                        process.Start();

                        lblProgress.Text = Path.GetFileName(sourceImage)
                            + "=>" + Path.GetFileName(newImage);
                        pbCurrentProgress.Value = i;
                        Application.DoEvents();
                        Thread.Sleep(100);
                    }
                }
                catch (Win32Exception ex)
                {
                    MessageBox.Show("Error on " + ex.Message, "Perhatian", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);   
                }
                
            }
            if (txtSourceFolder.Text.Length > 0)
            {
                Process.Start(txtSourceFolder.Text);
            }
            pbCurrentProgress.Value = 0;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            cancel = true;
        }
    }
}
